package com.freedom.admin.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.freedom.admin.domain.repository.RolePermissionRepository;
import com.freedom.admin.domain.service.RolePermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class RolePermissionServiceImpl implements RolePermissionService {

    private final RolePermissionRepository rolePermissionRepository;

    @Override
    public void addRolePermission(Integer roleId, List<Integer> permIds) {
        List<Integer> permissionIdList = rolePermissionRepository.findPermissionIdsByRoleId(roleId);
        if (CollUtil.isNotEmpty(permissionIdList)) {
            List<Integer> containsIdList = permissionIdList.stream().filter(permIds::contains).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(containsIdList)) {
                rolePermissionRepository.removeRolePermission(roleId, containsIdList);
            }
        }
        rolePermissionRepository.addRolePermission(roleId, permIds);
    }
}
